<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_ListPhotosTabsViewController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
    $this->view->is_ajax = $is_ajax = $this->_getParam('isajax', '');
    $this->view->showViewMore = $this->_getParam('showViewMore', 1);
    if (empty($is_ajax)) {
      $this->view->tabs = $tabs = Engine_Api::_()->getItemTable('seaocore_tab')->getTabs(array('module' => 'sitealbum', 'type' => 'photos', 'enabled' => 1));
      $count_tabs = count($tabs);
      if (empty($count_tabs)) {
        return $this->setNoRender();
      }
      $activeTabName = $tabs[0]['name'];
    }

    $getLightBox = Zend_Registry::isRegistered('sitealbum_getlightbox') ? Zend_Registry::get('sitealbum_getlightbox') : null;
    if (empty($getLightBox)) {
      return;
    }
    $this->view->marginPhoto = $this->_getParam('margin_photo', 12);
    $this->view->showLightBox = Engine_Api::_()->sitealbum()->showLightBoxPhoto();

    $parentTable = Engine_Api::_()->getItemTable('album');
    $parentTableName = $parentTable->info('name');
    $table = Engine_Api::_()->getItemTable('album_photo');
    $tableName = $table->info('name');
    $select = $table->select()
            ->from($tableName);
    if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.album_id', null);
    } else {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.collection_id', null);
    }
    $select->where($parentTableName . '.search = ?', true);
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.viewertype', 0)) {
      return $this->setNoRender();
    }
    $paramTabName = $this->_getParam('tabName', '');
    if (!empty($paramTabName))
      $activeTabName = $paramTabName;

    $activeTab = Engine_Api::_()->getItemTable('seaocore_tab')->getTabs(array('module' => 'sitealbum', 'type' => 'photos', 'enabled' => 1, 'name' => $activeTabName));

    $this->view->activTab = $activTab = $activeTab['0'];
    $type = null;
    switch ($activTab->name) {
      case 'recent_photos':
        $type = 'creation_date';
        break;
      case 'liked_photos':
        $type = 'like_count';
        $select->order($tableName . '.like_count DESC');
        break;
      case 'viewed_photos':
        $type = 'view_count';
        $select->order($tableName . '.view_count DESC');
        break;
      case 'commented_photos':
        $type = 'comment_count';
        $select->order($tableName . '.comment_count DESC');
        break;
      case 'featured_photos':
        $type = 'featured';
        $select->where($tableName . '.featured = ?', 1);
        $select->order('Rand()');
        break;
      case 'random_photos':
        $select->order('Rand()');
        break;
    }
    if (!Engine_Api::_()->sitealbum()->canShowSpecialAlbum())
      $select->where($parentTableName . '.type IS NULL');
    if ($activTab->name != 'featured_photos' && $activTab->name != 'random_photos') {
      $select->order($tableName . '.photo_id DESC');
    }

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    if (($paginator->getTotalItemCount() <= 0)) {
      return $this->setNoRender();
    }
    $this->view->activTab_limit=$activTab->limit;
    $paginator->setItemCountPerPage($activTab->limit);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
    $this->view->count = $paginator->getTotalItemCount();
    $this->view->params = $params = array();
    if ($this->view->showLightBox && !empty($type)) {
      $this->view->params = $params = array('type' => $type, 'count' => $paginator->getTotalItemCount(), 'title' => $activTab->getTitle() . " Photos");
    }
    $this->view->canCreate = Engine_Api::_()->authorization()->isAllowed('album', null, 'create');
  }

}

?>